import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { PageService } from './page.service';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import { DeletePageDto } from './dto/delete-page.dto';
import { SavePageDto, SavePageMDto } from './dto/save-page.dto';
@Controller('page')
@RequireLogin()
export class PageController {
  constructor(private readonly pageService: PageService) {}

  @Post('save_page')
  async savePage(
    @Body() savePage: SavePageDto,
    @UserInfo('userId') userId: number,
  ) {
    const param: SavePageMDto = {
      ...savePage,
      schema: savePage.schema || 'null',
      createdBy: userId,
      id: savePage.id || null,
    };
    return await this.pageService.save_page(param);
  }

  @Get('has_page')
  async hasPage(@Query('path') path: string) {
    return await this.pageService.has_page(path);
  }

  @Get('get_page_list')
  async getPageList(
    @Query('page') page: number,
    @Query('size') size: number,
    @Query('filter') filter: string,
    @Query('filterFields') filterFields: string = '',
    @Query('dev') dev: boolean = false,
  ) {
    return await this.pageService.get_page_list(
      page,
      size,
      filter,
      filterFields,
      dev,
    );
  }

  @Get('delete_page')
  async deletePage(@Query('id') id: number) {
    return await this.pageService.delete_page(id);
  }
  @Post('delete_batch_page')
  async deleteBatchPage(@Body() deletePage: DeletePageDto) {
    const { id } = deletePage;
    return await this.pageService.delete_batch_page(id);
  }

  @Get('get_page')
  async getPage(@Query('path') path: string) {
    return await this.pageService.get_page(path);
  }
}
